/**
 * 
 */
package com.et114.modules.products.card.services;

import java.util.List;

import com.et114.components.simpletable.DataList;
import com.et114.core.services.impl.BaseServicesImpl;
import com.et114.core.utility.FileUploadUtil;
import com.et114.modules.products.card.dao.CardDao;
import com.et114.modules.products.card.vo.Card;
import com.et114.modules.products.commons.Const;
import com.et114.modules.products.commons.Context;
import com.et114.modules.products.search.SearchService;
import com.et114.modules.products.search.SearchVo;

/**
 * @author guanhw
 */
public class CardServiceImpl extends BaseServicesImpl {
	private CardDao cardDao ;
	private SearchService   searchCompass ; 
	
	public void setCardDao ( CardDao cardDao ) {
		this.cardDao = cardDao;
	}

	public void setSearchCompass ( SearchService searchCompass ) {
		this.searchCompass = searchCompass;
	}

	public DataList getCards( Card vo ) throws Exception {
		return cardDao.getCards( vo ) ; 
	}
	
	public List< Card > getCardList( Card vo ) throws Exception {
		return cardDao.getCardList ( vo ) ; 
	}
	
	public List< Card  > getCardsHasOffTime( Card vo ) throws Exception { 
		return cardDao.getCardsHasOffTime ( vo ) ;
	}
	
	public Card getCardById( Long id ) throws Exception {
		return cardDao.getCardById ( id ) ; 
	}
	
	public Long saveCard( Card vo ) throws Exception {
		return cardDao.saveCard ( vo ) ; 
	}
	
	public void updateCard( Card vo ) throws Exception {
		cardDao.updateCard ( vo )  ;
		
		Card cp = getCardById( vo.getProId ( ) ) ; 
		if ( cp != null ) {
			if ( cp.getProOnOffStatus ( ) == Const.PRO_PUB_STATUS_PUBED ) {
				SearchVo svo = getCard4Compass ( cp.getProId ( ) ) ; 
				if ( svo != null ) 
					searchCompass.setPro  ( cp.getProId ( ) ,  Const.PRO_TYPE_CART  , svo ) ; 
			}
			else 
				searchCompass.removePro  ( cp.getProId ( ) , Const.PRO_TYPE_CART ) ; 				
		}		

	}
	
	public void updateCardProOnOffStatus( Card cp ) throws Exception {
		cardDao.updateCardProOnOffStatus ( cp ) ; 
		
		if ( cp != null ) {
			if ( cp.getProOnOffStatus ( ) == Const.PRO_PUB_STATUS_PUBED ) {
				SearchVo svo = getCard4Compass ( cp.getProId ( ) ) ; 
				if ( svo != null ) 
					searchCompass.setPro  ( cp.getProId ( ) ,  Const.PRO_TYPE_CART  , svo ) ; 
			}
			else 
				searchCompass.removePro  ( cp.getProId ( ) , Const.PRO_TYPE_CART ) ; 				
		}				
	}
	
	public void removeCard( Long proId ) throws Exception {
		//delete compass
		SearchVo svo = getCard4Compass ( proId ) ; 
		if ( svo != null )
			searchCompass.removePro  ( proId , Const.PRO_TYPE_CART ) ; 
		
		
		
		Card card = getCardById( proId ) ; 
		if ( card != null  ) {
			FileUploadUtil.removeFile ( card.getCardImg ( ) ) ; 
			cardDao.removeCard ( proId ) ;	
		}
		 
		Context.removeProContent ( proId , Const.PRO_TYPE_CART ) ; 
	}
	
	// 4 q
	public DataList getCards4View( Card vo ) throws Exception {
		return cardDao.getCards4View ( vo ) ;
	}
	
	public Card getBaseCardById4View( Long id ) throws Exception {
		return getBaseCardById4View ( id , Const.PRO_PUB_STATUS_PUBED ) ;
	}		
	
	public Card getBaseCardById4View ( Long id , Byte proOnOffStatus )
			throws Exception {
		Card card = new Card ( );
		card.setProId ( id );
		card.setProOnOffStatus ( proOnOffStatus );
		return cardDao.getBaseCardById4View ( card );
	}
	
	public SearchVo getCard4Compass( Long proId ) {
		return cardDao.getCard4Compass ( proId ) ; 
	}
}
